package yao.algorithm.personal;

public class ChineseNumConverter {
	
	
	private static final String[] chnNumChar = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
	private static final String[] chnUnitSection = {"", "万", "亿", "万亿"};
	
	private static final String[] chnUnitChar = {"", "十", "百", "千"};
	
	public static void main(String[] args) {
		//3200020
		System.out.println(numberToChinese(3210021, new StringBuilder("")));
		System.out.println(2/10);
	}

	public static String toChinese(int num) {
		
		if(num == 0) {
			return chnNumChar[0];
		}
		
		StringBuilder chineseNum = new StringBuilder("");
		//1.已万为单位分节，并确定节权位
		//2.对每个小节内数字确定权位
		//3.处理零的情况
		int pos = 0;
		if(num >= 10000) {
			num = num / 10000;
		} else {
			
		}
		return null;
	}
	
	
	private static StringBuilder sectionToChinese1(int section, StringBuilder chnStr) {
		StringBuilder result = new StringBuilder("");
		int pos = 0;
		while(section > 0) {
			int v = section % 10;
			if(v == 0) {
				
			} else {
				result.insert(0, chnNumChar[v]);
			}
		}
		pos++;
		
		return chnStr;
	}
	
	
	
	public static String numberToChinese(int num, StringBuilder chnStr) {
		//1.对阿拉伯数字分节,并确定节权位名称，num对10000去模可得到一个section
		//2.section转成中文数字
		//3.然后根据节的位置补上节权位
		//重复这个过程，知道num == 0
		//对0做特殊处理
		if(num == 0) {
			return chnNumChar[0];
		}
		int unitPos = 0; 	//记录节的位置，0:"" 1:"万" 2："亿"，全0的节不需要节权位
		boolean needZero = false;	//是否需要补“零”
		StringBuilder strIns = new StringBuilder("");	//最后结果
		
		while(num > 0) {
			int section = num % 10000;
			if(needZero) {
				chnStr.insert(0, chnNumChar[0]);
			}
			
			sectionToChinese(section, strIns);
			
			strIns.append(section != 0 ? chnUnitSection[unitPos] : chnUnitSection[0]);
			chnStr.insert(0, strIns.toString());
			
			needZero = (section < 1000) && (section > 0);
			num = num / 10000;
			
			unitPos++;
		}
		return chnStr.toString();
	}
	
	
	
	private static void sectionToChinese(int section, StringBuilder chnStr) {		
		String strIns;
		int unitPos = 0;
		boolean zero = true;
		
		while(section > 0) {
			int v = section % 10;
			if(v == 0) {
				if(section == 0 || !zero) {
					zero = true;	//需要补，zero的作用是确保对连续的多个，只补一个中文零
					chnStr.insert(0, chnNumChar[v]);
				}
			} else {
				zero = false;	//至少有一个数字不是
				strIns = chnNumChar[v];	//此位对应的中文数字
				strIns += chnUnitChar[unitPos];	//此位对应的中文权位
				chnStr.insert(0, strIns);
			}
			unitPos++;	//移位
			section = section / 10;	
		}
	}

	

}
